import java.io.File;
import java.util.*;
import java.io.IOException;
import java.util.List;

import org.dom4j.DocumentException;

import comp.ModelBuilder;
import comp.PatternLoader;
import comp.PatternLoader.FileType;
import comp.TrainingSetGenerator;

import recoder.CrossReferenceServiceConfiguration;
import recoder.ParserException;
import recoder.abstraction.ClassType;
import recoder.abstraction.Method;
import recoder.abstraction.Type;
import recoder.convenience.ForestWalker;
import recoder.io.DefaultProjectFileIO;
import recoder.io.SourceFileRepository;
import recoder.java.CompilationUnit;
import recoder.java.ProgramElement;
import recoder.java.declaration.ClassDeclaration;
import recoder.java.declaration.Extends;
import recoder.java.declaration.FieldDeclaration;
import recoder.java.declaration.FieldSpecification;
import recoder.java.declaration.Implements;
import recoder.java.declaration.InterfaceDeclaration;
import recoder.java.reference.TypeReference;
import recoder.service.SourceInfo;
import util.CsvWriter;
import util.MetricsWriter;
import util.PatternXMLParser;
import dpdetector.DetectorManager;
import dpdetector.DetectorManager.DetectorEnum;
import dpdetector.test.helper.PathHelper;
import dpdetector.util.RelationJudgeUtil;
import entity.Metrics;


public class Main {
	
	public static void main(String[] args) throws ParserException, IOException, ClassNotFoundException, DocumentException{
		//initialization of the AST
		
		
		//String inputFile = PathHelper.getTestDir()+"JHotDraw7.0.6/jhotdraw.prj";
		//String inputFile = PathHelper.getTestDir()+"J2SE5/J2SE5.prj";
		//String inputFile = PathHelper.getTestProject();
		
		
		String inputFile = PathHelper.getTestProject2();
		
		String metricsOutputPath = "./data/j2se5_metrics.csv";
		String patternVectorOutputPath = "./data/j2se5_train_data_manual.csv";
		
		
		ModelBuilder mb = new ModelBuilder(inputFile);
		Map<String,Metrics> metricsMap = mb.extractMetrics();
			
    	//stdout
		int n = 0;
    	for(Metrics m: metricsMap.values()){
    		if(m.getNoi()!=0 || m.getNos()!=0 || m.getNodr()!=0|| m.getNode()!=0){
    			
    			n++;
    		}
    		System.out.println(m);		
    	}
    	
    	PatternLoader pl = new PatternLoader("./repo",FileType.PlainText);
    	Map<String,Set<Enum>> patternMap = pl.load("\t");
    	
//    	for(String name : (patternMap.keySet())){
//    		System.out.println(name);
//    	}
//    	System.out.println(patternMap.size());
//    	
    	
    	TrainingSetGenerator tsg = new TrainingSetGenerator(metricsMap,inputFile,patternMap);
    	tsg.generateTrainingData();
    	
    	
		//for(String s : tsg.getTrainData().values()) System.out.println(s);
    	
    	
	
    	//write metrics to file
    	MetricsWriter writer = new MetricsWriter(metricsMap.values());
    	writer.setOutputFileName(metricsOutputPath);
    	//writer.setHeader(false);
    	//writer.setCondition("java.io");
    	//writer.writeCsv();
    	
    	//write corresponding result to another file
    	//CsvWriter.write(patternVectorOutputPath, tsg.getTrainData());
	
	}
	

	
	public static void invokePreviousDPD(String inputFilePath, String repo) throws ParserException, IOException{
		//choose to generate or not
		
		DetectorManager detector = new DetectorManager(inputFilePath);
		detector.addDetector(DetectorEnum.COMPOSITE);
		detector.addDetector(DetectorEnum.ADAPTER);
		detector.addDetector(DetectorEnum.DECORATOR);
		detector.buildAnalysisModel();
		detector.detect();
		detector.setOutputDirPath(repo);
		detector.outputXMLFile();
	}
	
	
	

}
